package com.example.mythealthapp.users

import android.content.ContentValues
import android.content.ContentValues.TAG
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import com.example.mythealthapp.R
import kotlinx.android.synthetic.main.activity_register.*

class RegisterActivity : AppCompatActivity() {

    private val helper: DBHelper = DBHelper(this, "User", 1)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)

        val userInputWatcher = object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"beforeTextChanged:$p0")
            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                Log.i(TAG,"onTextChanged:$p0")
            }
            override fun afterTextChanged(p0: Editable?) {
                Log.i(TAG,"afterTextChanged:$p0")
            }
        }

        register_EditText_userphone.addTextChangedListener(userInputWatcher)
        register_EditText_username.addTextChangedListener(userInputWatcher)
        register_EditText_pass.addTextChangedListener(userInputWatcher)
        register_EditText_repass.addTextChangedListener(userInputWatcher)

        register_button_submit.setOnClickListener {
            val phone = register_EditText_userphone.text.toString()
            val exist:Boolean = isExsist(phone)
            if(exist){
                Toast.makeText(this,"该手机已注册",Toast.LENGTH_LONG)
                    .show()
            }else{
                val pass=register_EditText_pass.text.toString()
                val repass=register_EditText_repass.text.toString()
                if(pass==repass)
                {
                    Log.d(TAG,"pass==repass")
                    val username=register_EditText_username.text.toString()
                    val rs= toRegist(phone,username,pass,System.currentTimeMillis())
                    if(rs){
                        Toast.makeText(this,"注册成功",Toast.LENGTH_LONG)
                            .show()
                        val intent= Intent(this,LoginActivity::class.java)
                        finish()
                        startActivity(intent)
                    }else{
                        Toast.makeText(this,"出了点小意外，再试试吧",Toast.LENGTH_LONG)
                            .show()
                    }
                }
               else{
                    Toast.makeText(this,"密码输入不一致",Toast.LENGTH_LONG)
                        .show()
               }
            }
        }
    }

    private fun toRegist(phone: String, username: String, userpass: String, currentTimeMillis: Long) :Boolean {
        val db=helper.writableDatabase
        val newuser = User(username,phone,userpass,0,0,currentTimeMillis,0)
        val values = ContentValues().apply {
            put(User.U_phone,newuser.userphone)
            put(User.U_name,newuser.username)
            put(User.U_pass,newuser.userpass)
            put(User.U_addtime,newuser.addtime)
            put(User.U_level,newuser.level)
        }
        var rs = -1
        rs = db.insert(User.TABLE,null,values).toInt()
        return rs>0
    }

    //判断用户是否已存在
    private fun isExsist(phone: String): Boolean {
        val db=helper.readableDatabase
        val cursor = db.query(User.TABLE,null, User.U_phone+"=?", arrayOf(phone),null,null,null,null)
        if(cursor.moveToFirst()){ return true }
        return false
    }




    override fun onStart(){
        super.onStart()
        println("onStart")
        Log.d(TAG,"onDestory")
    }
    override fun onPause(){
        super.onPause()

        println("onPause")
        Log.d(TAG,"onPause")
    }
    override fun onRestart(){
        super.onRestart()
        println("onRestart")
        Log.d(TAG,"onRestart")
    }
    override fun onResume(){
        super.onResume()
        println("onResume")
        Log.d(TAG,"onResume")
    }
    override fun onStop(){
        super.onStop()
        println("onStop")
        Log.d(TAG,"onStop")
    }
    override fun onDestroy(){
        super.onDestroy()
        println("onDestroy")
        Log.d(TAG,"onDestory")
    }

}